//绑定下拉事件
function bindEvent(node) {
    $(node).change(flushTestTable);
}

//设置题目数量的显示
function setLabel(event) {
    var label=$(this).next()[0];
    label.innerHTML=this.value+"/"+this.max;
}

//增加一个range
function addRange(name,max) {
    var group=$("<div class='form-group'>\n" +
        "    <label class='col-sm-4 control-label'>"+name+"</label>\n" +
        "    <div class='col-sm-6 range'>\n" +
        "        <input type='range' min='0' max='"+max+"' value='0'>\n" +
        "        <label></label>\n" +
        "    </div>\n" +
        "</div>");

    $("#modal-test").find(".modal-body").append(group);
}

//添加章节
function addSection() {
    //获取相关信息
    var subject=$("#subject").find("option:selected");
    var volumn=$("#volume").find("option:selected");
    var section=$("#section").find("option:selected");

    //获取选择内容
    if(section.val()==="请选择"){
        alert("请选择章节");
        return;
    }

    //添加进table
    var table=$("#table-section").find("tbody");
    var tr=$("<tr>\n" +
        "    <td>"+subject.text()+"</td>\n" +
        "    <td>"+volumn.text()+"</td>\n" +
        "    <td>"+section.text()+"</td>\n" +
        "    <td>\n" +
        "        <button class='btn btn-sm btn-danger'>\n" +
        "            <span class='glyphicon glyphicon-remove'></span>\n" +
        "        </button>\n" +
        "        <input type='hidden' class='section-val' value='"+section.val()+"'>\n" +
        "    </td>\n" +
        "</tr>");
    
    tr.find(".btn").click(function () {
        tr.remove();
    });

    table.append(tr);

    $("#modal-section").modal("hide");
}

//获取到的所有数据的json
var allData;
//题库json
var testLibrary;
//已选择的json
var testSelected=[];

//获取二级表单的数据并且打开modal
function openModalTest() {
    var json={"sectionid":[]};

    var sections=$("#table-section").find("tbody").find("input[type='hidden']");

    //把所有数据装载到一个json那里
    sections.each(function () {
        json.sectionid.push($(this).val());
    });

    //获取题目总数
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-questions-questionlist",json,function (data) {
        var json=JSON.parse(data);

        if(json.code==0){
            allData=json.data;
            $("#modal-test").modal();
            flushTestTable()
        }else {
            alert(json.msg);
        }
    })
}

//点击添加题目事件
function addTest() {
    var selected=$(".test-library").find("select").val();
    for(var i=0;i<selected.length;i++){
        testSelected.push(testLibrary[selected[i]]);
    }
    flushTestTable();
    flushTestSelected();
}

//刷新题目选择列表
function flushTestTable() {
    var selectLibrary=$(".test-library").find("select");
    selectLibrary.empty();

    var level=$(".test-level").val();
    var type=$(".test-type").val();

    testLibrary=[];

    for(var i=0;i<allData.length;i++){
        if(allData[i].type==type&&allData[i].level==level
        &&testSelected.indexOf(allData[i])==-1){
            testLibrary.push(allData[i]);
        }
    }

    //剔除已经选择的题目
    for(var i=0;i<testLibrary.length;i++){
        var msg=testLibrary[i].section+"　　"+testLibrary[i].question;
        var option=$("<option value='"+i+"'>"+msg+"</option>");
        option.dblclick(getTestDetail);
        selectLibrary.append(option);
    }
}

//刷新已选择列表
function flushTestSelected() {
    var node=$(".test-selected");
    node.empty();
    for(var i=0;i<testSelected.length;i++){
        var msg=testSelected[i].section+"　　"+testSelected[i].question;
        var option=$("<div value='"+i+"' class='tag label label-info block'>"+msg+"<span data-role='remove'></span></div>");
        option.click(removeTestSelected);
        node.append(option);
    }
}

//删除已选择的题目
function removeTestSelected() {
    var value=$(this).attr("value");
    testSelected.splice(value,1);

    flushTestSelected();
    flushTestTable();
}

//双击选项事件,获取题目详情
function getTestDetail() {
    var test=testLibrary[$(this).val()];
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-questions-questioninfo",{id:test.id},function (data) {
        data=JSON.parse(data).data;
        var parent=$(".test-detail");

        setTopicDetail(data,parent);
    });
}



//提交题集表单
function submitTestSet() {
    var name=$("#examname").val();
    if(name==""){
        alert("题集名称不能为空!");
        return;
    }

    var questionId=[];
    for(var i=0;i<testSelected.length;i++){
        questionId.push(testSelected[i].id);
    }

    var json={"examname":name,"questionid[]":questionId};

    console.log(json);
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-questions-addexam",
        json,function (data) {
        data=JSON.parse(data);
        console.log(data);
        if(data.code!=0){
            alert(data.msg);
        }else {
            testSelected=[];
            flushTestSelected();
            alert("出题成功！");
        }
    })
}